package com.device.Controller;

import com.device.Constant.BaseConstant;
import com.device.Entity.Dto.SessionWebUserDto;
import com.device.Entity.User;
import com.device.Service.UserService;
import com.device.annotation.GlobalInterceptor;
import com.device.common.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/")
@RestController
public class AccountController {
    @Resource
    private UserService userService;
    @RequestMapping("login")
    public Result login(HttpSession httpSession, String username,String password,Integer roles){
        SessionWebUserDto sessionWebUserDto=userService.login(username,password,roles);
        httpSession.setAttribute(BaseConstant.key,sessionWebUserDto);
        return Result.success(sessionWebUserDto);

    }
    @RequestMapping("register")
    public Result register(String username,String password,Integer roles){
        userService.register(username,password,roles);
        return Result.success();

    }
    @RequestMapping("info")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result info(HttpSession httpSession){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        return Result.success(sessionWebUserDto);

    }
    @RequestMapping("updateinfo")
    @GlobalInterceptor(checkParam = true,checkLogin = true)
    public Result updateinfo(HttpSession httpSession,String password,String name,String avatar){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        User user=userService.getById(sessionWebUserDto.getId());
        user.setPassword(password);
        user.setName(name);
        user.setAvatar(avatar);
        userService.updateById(user);
        sessionWebUserDto.setAvatar(avatar);
        sessionWebUserDto.setName(name);
        sessionWebUserDto.setPassword(password);
        httpSession.setAttribute(BaseConstant.key,sessionWebUserDto);
        return Result.success();

    }

}
